﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Windows.Forms;
using Assist;
using Autodesk.Revit.DB;
using Autodesk.Revit.UI;
using YArchitech.Controls;
using YArchitech.LIB.YJKControls;

namespace YJKRevitWndr
{
	internal partial class DlgDoorWinTagInsert : System.Windows.Forms.Form
	{
		public DlgDoorWinTagInsert(UIApplication uiApp)
		{
			try
			{
				this.m_application = uiApp;
				this.m_document = uiApp.ActiveUIDocument.Document;
				this.fInfo = Assist.FileUtil.GetTempDataFile();
				this.InitializeComponent();
				this.InitType();
				this.InitSelect();
			}
			catch (Exception ex)
			{
				YJKMessageBox.Show(ex.Message);
			}
		}

		private void InitSelect()
		{
			if (Assist.FileUtil.GetDataFromTempFile(this.fInfo.FullName, "门窗编号", "是否选择集编号") == "是")
			{
				this.rbSelection.Checked = true;
				if (Assist.FileUtil.GetDataFromTempFile(this.fInfo.FullName, "门窗编号", "类型过滤") == "是")
				{
					this.cbByType.Checked = true;
					return;
				}
			}
			else
			{
				this.rbSingle.Checked = true;
			}
		}

		private void InitType()
		{
			this.FindTagSymbol();
			if (Assist.FileUtil.GetDataFromTempFile(this.fInfo.FullName, "门窗编号", "是否窗编号") == "是")
			{
				this.rbtWin.Checked = true;
				this.cbxSymbol.Items.Clear();
				this.cbxSymbol.Items.Add("窗标记");
				this.cbxSymbol.SelectedIndex = 0;
				return;
			}
			this.rbtDoor.Checked = true;
			this.cbxSymbol.Items.Clear();
			this.cbxSymbol.Items.Add("门标记");
			this.cbxSymbol.SelectedIndex = 0;
		}

		private void FindTagSymbol()
		{
			foreach (Element element in new FilteredElementCollector(this.m_document).OfClass(typeof(Family)).ToElements())
			{
				Family family = (Family)element;
				try
				{
					if (family.FamilyCategory.Id.IntegerValue == -2000450)
					{
						this.m_lstWindowTagFamilys.Add(family);
					}
					else if (family.FamilyCategory.Id.IntegerValue == -2000460)
					{
						this.m_lstDoorTagFamilys.Add(family);
					}
				}
				catch (Exception)
				{
				}
			}
		}

		private void rbSingle_CheckedChanged(object sender, EventArgs e)
		{
			if (this.rbSingle.Checked)
			{
				this.cbByType.Checked = false;
				this.cbByType.Enabled = false;
			}
		}

		private void rbSelection_CheckedChanged(object sender, EventArgs e)
		{
			if (this.rbSelection.Checked)
			{
				this.cbByType.Checked = false;
				this.cbByType.Enabled = true;
			}
		}

		private void tbxTag_MouseClick(object sender, MouseEventArgs e)
		{
		}

		private void tbxTag_Leave(object sender, EventArgs e)
		{
		}

		private void btnOK(object sender, EventArgs e)
		{
			Dictionary<string, string> dictionary = new Dictionary<string, string>();
			if (this.rbtDoor.Checked)
			{
				if (this.m_lstDoorTagFamilys.Count != 0)
				{
					this.m_useTag = this.m_lstDoorTagFamilys.First<Family>();
				}
				dictionary.Add("是否窗编号", "否");
				dictionary.Add("门标记样式", this.cbxSymbol.Text);
			}
			else
			{
				if (this.m_lstWindowTagFamilys.Count != 0)
				{
					this.m_useTag = this.m_lstWindowTagFamilys.First<Family>();
				}
				dictionary.Add("是否窗编号", "是");
				dictionary.Add("窗标记样式", this.cbxSymbol.Text);
			}
			if (this.rbSingle.Checked)
			{
				dictionary.Add("是否选择集编号", "否");
			}
			else
			{
				dictionary.Add("是否选择集编号", "是");
			}
			if (this.cbByType.Checked)
			{
				dictionary.Add("类型过滤", "是");
			}
			else
			{
				dictionary.Add("类型过滤", "否");
			}
			Assist.FileUtil.WriteTempData(this.fInfo.FullName, "门窗编号", ConfigData.DoorWinTagData, dictionary);
		}

		private void rbtDoor_CheckedChanged(object sender, EventArgs e)
		{
			this.cbxSymbol.Items.Clear();
			if (this.rbtDoor.Checked)
			{
				this.m_strTagType = "门标记";
				this.cbxSymbol.Items.Clear();
				this.cbxSymbol.Items.Add("门标记");
				this.cbxSymbol.SelectedIndex = 0;
			}
		}

		private void rbtWin_CheckedChanged(object sender, EventArgs e)
		{
			this.cbxSymbol.Items.Clear();
			if (this.rbtWin.Checked)
			{
				this.m_strTagType = "窗标记";
				this.cbxSymbol.Items.Clear();
				this.cbxSymbol.Items.Add("窗标记");
				this.cbxSymbol.SelectedIndex = 0;
			}
		}

		private void DlgDoorWinTagInsert_Load(object sender, EventArgs e)
		{
		}

		private UIApplication m_application;

		private Document m_document;

		private List<Family> m_lstWindowTagFamilys = new List<Family>();

		private List<Family> m_lstDoorTagFamilys = new List<Family>();

		public Family m_useTag;

		private string m_strTagType = string.Empty;

		private FileInfo fInfo;
	}
}
